﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using System.Data;
using System.Text;
using ZHDJ.WebPage;
using ZHDJ.Core.Caching;
using ZHDJ.Data.Models.Org;
using AppBox.BLL;
using Newtonsoft.Json;
using ZHDJ.Data;
using System.Web.Services;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;

namespace ZHDJ.Web
{
    public partial class Map : System.Web.UI.Page
    {
        public static AppBoxContext DB
        {
            get
            {
                if (!HttpContext.Current.Items.Contains("__AppBoxContext"))
                {
                    HttpContext.Current.Items["__AppBoxContext"] = new AppBoxContext();
                }
                return HttpContext.Current.Items["__AppBoxContext"] as AppBoxContext;
            }
        }



        StringBuilder sbChild = new StringBuilder();
        protected List<PartyOrgBranch> branchList = new List<PartyOrgBranch>();
        protected string json = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree();
            }
        }

        /// <summary>
        /// 绑定组织Tree
        /// </summary>
        public void BindTree()
        {
            StringBuilder sb = new StringBuilder();
            TreeControlBusiness tree = new TreeControlBusiness("BranchID", "ParentBranchID", "BranchName",
                true, CacheKeys.Cache_Branch_Tree_Navigation);
            var TreeBranch = new FineUI.Tree();
            tree.BindTreeBranch(TreeBranch);

            foreach (var item in TreeBranch.Nodes)
            {
                var data = new TreeData() { id = item.NodeID, name = item.Text, level = 0 };
                sb.Append("<li><span class=\"folder\">" + data.name + "</span>");

                BindTreeChild(item.Nodes, data);

                sb.Append(sbChild.ToString());
                sb.Append("</li>");
            }
            liTree.Text = sb.ToString();
            json = JsonConvert.SerializeObject(branchList);
        }

        private void BindTreeChild(FineUI.TreeNodeCollection treeNodes, TreeData treeData)
        {
            if (treeNodes == null)
                return;

            foreach (var item in treeNodes)
            {
                sbChild.Append("<ul>");
                var data = new TreeData() { id = item.NodeID, name = item.Text, level = treeData.level + 1 };

                if (item.Nodes.Count == 0)
                {
                    var model = DB.PartyOrgBranchs.Find(data.id);
                    branchList.Add(model);
                    if (!string.IsNullOrWhiteSpace(model.XPoint) && !string.IsNullOrWhiteSpace(model.YPoint))
                    {
                        sbChild.Append("<li><span style=\"cursor: pointer;\" class=\"file\" onclick=\"popOrginfoDialog('" + model.BranchName + "','" + model.LinkMan + "','" + model.LinkPhone + "','" + model.Address + "'," + model.XPoint + "," + model.YPoint + ",'" + model.ImageUrl + "','" + model.BranchID + "', true)\" >" + data.name + "</span>");
                    }
                    else
                    {
                        sbChild.Append("<li><span style=\"cursor: pointer;\"  class=\"file\">" + data.name + "</span>");
                    }
                }
                else
                    sbChild.Append("<li><span class=\"folder\">" + data.name + "</span>");

                BindTreeChild(item.Nodes, data);
                sbChild.Append("</li>");
                sbChild.Append("</ul>");
            }
        }

        /// <summary>
        /// 根据关键字搜索组织信息
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public static string ScreachOrgByKeyword(string keyword)
        {
            var models = DB.PartyOrgBranchs.Where(o => o.BranchName.Contains(keyword)).ToList();
            List<string> ids = models.Select(o => o.BranchID).ToList();
            var childs = DB.PartyOrgBranchs.Where(o => ids.Contains(o.ParentBranchID)).ToList();
            List<string> parentIds = childs.Select(o => o.ParentBranchID).ToList();
            for (int i = 0; i < models.Count; i++)
            {
                if (parentIds.Contains(models[i].BranchID))
                {
                    models.RemoveAt(i);
                    i--;
                }
            }

            return JsonConvert.SerializeObject(models);
        }

        /// <summary>
        /// 获取组织图片
        /// </summary>
        /// <param name="imgId"></param>
        /// <returns></returns>
        [WebMethod]
        public static string GetImgURLbyImgId(string imgId)
        {
            return UpFileBusiness.GetFileUrl(imgId);
        }

        /// <summary>
        /// 获取活动列表
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public static string GetActivitieTitle(string branchId, int pageSize, int startIndex)
        {
            //var ds = PartyArticle.GetMoreArticleTitle(pageSize, startIndex, branchId, "CadreWork", "Dynamic", true);
            var ds = PartyVolunteerActivitie.GetMoreActivitieTitle(pageSize, startIndex, branchId);
            return JsonConvert.SerializeObject(ds.Tables[0]);
        }

        /// <summary>
        /// 获取上级精神
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public static string GetSuperiorsSpirit(string branchId, int pageSize, int startIndex)
        {
            var ds = PartyArticle.GetMassLineSuperiorSpirit(startIndex, pageSize, "", "MassLine", "SuperiorsSpirit");
            return JsonConvert.SerializeObject(ds.Tables[0]);
        }
    }
}